//
//  ZFSubLayerView.swift
//  map
//
//  Created by fyzq on 2024/1/30.
//

import UIKit
import Kingfisher
import RxSwift
import CollectionKit
class ZFSubLayerView: ZFBaseView {
    var subject = PublishSubject<ZFLayerModal>()
    
    let collectionView = CollectionView()
    override init(frame: CGRect) {
        super.init(frame: frame)
        setZFSubLayerView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func setZFSubLayerView(){
        
        let dataArr =  ZFLayerModal.getLayers()
        let dataSource = ArrayDataSource(data: dataArr)
       
        let provider = BasicProvider(
            dataSource: dataSource,
            viewSource: {(view: ZFLayerCollectionView, data: ZFLayerModal, index: Int) in
                view.modal = data
            },
            sizeSource: {(_,_,_ )-> CGSize in
                let width = UIDevice.width - 30;
                let item_width = (width - 4 * 10) / 3.0
                return CGSize(width: item_width, height: 80.0)
            },
            layout: FlowLayout(spacing: 10,alignItems: .start).inset(by: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))) {[weak self] context in
                guard let self = self else {return}
                self.subject.onNext(context.data)
            }
        collectionView.provider = provider;
        addSubview(collectionView)
        
        var column =  dataSource.data.count / 5
        if dataSource.data.count % 5 != 0 {
            column += 1
        }
        
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.height.equalTo((20 + column * 80 + (column - 1) * 10 ))
        }

                
    }
    
}


class ZFLayerCollectionView :ZFBaseView {
    var modal:ZFLayerModal!{
        didSet{
            imageView.kf.setImage(with: URL(string:modal.picture!))
            titleLabel.text = modal.mapName
        }
    }
    private var imageView:UIImageView!
    private var titleLabel:UILabel!
    override init(frame: CGRect) {
        super.init(frame: frame)
        setZFLayerCollectionCell()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    fileprivate func setZFLayerCollectionCell(){
        
        imageView =  UIImageView()
        addSubview(imageView)
        imageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        
        titleLabel = UILabel()
        titleLabel.textAlignment = .center
        titleLabel.font = UIFont.systemFont(ofSize: 10)
        titleLabel.textColor = R.color.color_66()
        addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(22)
        }
        titleLabel.backgroundColor = R.color.color_f1()
        addRadius(radius: 5)
        
    }
}
